module.exports  = app => {
  const express = require('express')
  const router = express.Router({
    mergeParams: true
  })

  //增加
  router.post('/', async (req, res) => {
    const model = await req.Model.create(req.body)
    res.send(model)
  })
  //获取
  router.get('/', async (req, res) => {
    const model = await req.Model.find()
    res.send(model)
  })
  //获取部分
  router.get('/:page/:count', async (req, res) => {
    // console.log(req.params.count, req.params.page)
    let { page, count } = req.params
    page = parseInt(page)
    count = parseInt(count)
    let skip = (page - 1) * count
    const model = await req.Model.find().skip(skip).limit(count)
    res.send(model)
  })
  //删除
  router.delete('/:id', async (req, res) => {
    const model = await req.Model.findByIdAndDelete(req.params.id)
    res.send({
      success: true
    })
  })
  //多条删除
  router.post('/del', (req, res) => {
    req.body.forEach(async (item) => {
      const model = await req.Model.findByIdAndDelete(item)
    })
    res.send({
      success: true
    })
  })
  //部分查询
  router.post('/query', (req, res) => {
    res.send({
      success: true
    })
  })
  //修改
  router.put('/:id', async (req, res) => {
    const model = await req.Model.findByIdAndUpdate(req.params.id, req.body)
    res.send(model)
  })

  const multer = require('multer')
  let upload = multer({dest: __dirname + `/../../upload`})


  app.use('/upload', upload.single('file'), async (req, res) => {
    const file = req.file
    file.url = `http://localhost:3000/upload/${file.filename}`
    res.send(file)
  })

  app.use('/admin/api/:resource', async (req, res, next) => {
    const map = {
      account: 'Account',
      basic: 'Basic',
      group: 'Group',
      house: 'House',
      job: 'Job',
      subsidy: 'Subsidy',
      village: 'Village',
      cadre: 'Cadre',
      party: 'Party',
      org: 'Org',

      resource: 'Resource',
      assets: 'Assets',
      finan: 'Finan',
      funds: 'Funds',


      village_announ: 'Announ',
      village_meeting: 'Meeting',
      village_plan: 'Plan',
      "org_act": 'Org_act',
      village_act: 'Village_act',
      

      admin: 'Admin',
    }
    console.log(req.params.resource)
    console.log(map[req.params.resource])
    req.Model = require(`../../models/${map[req.params.resource]}`)
    next()
  },router)

  
}